<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\model\DetailModel;
use app\model\OrderModel;
use app\model\UserModel;
use app\model\WareModel;
use think\facade\Request;
use think\facade\Validate;

class Order
{
    public function index()
    {
        $orderList=OrderModel::select();

        foreach ($orderList as &$row){
            $user = UserModel::find($row['user_id']);
            $row['user_name'] = $user['user_name'];     //用户名称赋值给当前订单
        }
        unset($row);

        $data=[
            'status'=>0,
            'message'=>'',
            'data'=>[
                'orderList'=>$orderList,
            ],
        ];
        return json($data);
    }
    //增加订单
    public function add(){
        $params=Request::param();
        $validate=Validate::rule([
            'user_id|用户id' =>'require|between:1,' . PHP_INT_MAX,
            // 'ware_id|商品id' => 'require|between:1,' . PHP_INT_MAX,
            'num|数量' => 'require|between:1,' . PHP_INT_MAX,
            'order_status|订单状态' => 'require|min:2|max:45',
            'pay_status|支付状态' => 'require|min:2|max:45',
            'order_image|图片' => 'require|min:2|max:300',
            'order_name|名称' => 'require|min:2|max:45',
            'order_price|应付金额' => 'require|between:1,' . PHP_INT_MAX
        ]);
        if (!$validate->check($params)){
            $data=[
                'status'=>'1',
                'message'=>$validate->getError(),
                'data'=>[]
            ];
            return json($data);
        }
        $params['update_time']=$params['add_time']=time();
        $result=OrderModel::create($params);
        $data=[
            'status'=>'0',
            'message'=>'success',
            'data'=>[
                'orderList'=>$result->toArray()
            ]
        ];
        return json($data);
    }
    public function list(){
        $userId=Request::param('user_id');
        $validate=Validate::rule([
            'user_id|所属用户id'=>'require|between:1,'. PHP_INT_MAX
        ]);
        if (!$validate->check(['user_id'=>$userId])){
            $data=[
                'status'=>'1',
                'message'=>$validate->getError(),
                'data'=>[]
            ];
            return json($data);
        }
        $result=OrderModel::where("user_id","=",$userId)
            ->order("add_time","desc")
            ->select();
        $orderList=[];
        foreach ($result as $item){
            $temp=$item->toArray();
            $orderList[]=[
                'order_id'=>$temp['order_id'],
                'user_id'=>$temp['user_id'],
                'num'=>$temp['num'],
                'order_price'=>$temp['order_price'],
                'order_status'=>$temp['order_status'],
                'add_time'=>$temp['add_time'],
            ];
        }

        foreach ($orderList as &$row){
            $detail = DetailModel::where('order_id',"=", $row['order_id'])->select();
            foreach ($detail as &$item){
                $ware = WareModel::find($item['ware_id']);
                $item['ware_name'] = $ware['ware_name'];
                $item['ware_price'] = $ware['ware_price'];
                $item['ware_image'] = $ware['ware_image'];

            }
            unset($item);

            $row['detail'] = $detail;
        }
        unset($row);
        
        $data=[
            'status'=>'0',
            'message'=>'',
            'data'=>[
                'orderList'=>$orderList
            ]
        ];
        return json($data);
    }
    public function isCanShow(){
        return 1;
    }
   
}
